
const { goodList, getUser, getCode } = require("../../utils/api");

// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    pointList: [],
    tabList: [
      { id: 0, title: '全部', type: 'sort', },
      { id: 1, title: '价格', type: 'score', shunxu: false },
      { id: 2, title: '兑换量', type: 'exc_count', shunxu: true }],
    activeTab: 0,
    sortType: '', //筛选类型
    searchValue: '',
    isJwt: false,
    showLogin: false,
    page: 1,
    limit: 10,
    count: null,
    onBottom: false,//是否到底
    descasc: '',
    fatherData: {},
    showModal: false,
    showErrModal: false,
    errData: '',
    myData: ''
  },


  onLoad() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300,
    })

    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }

  },

  getPageData() {
    let that = this
    const jwt = wx.getStorageSync('jwt');
    if (jwt) {
      that.setData({
        isJwt: true
      })
    } else {
      that.setData({
        isJwt: false
      })
    }

    if (that.data.isJwt) {
      that.getUserInfo()
    }
  },

  onShow() {
    this.setData({
      page: 1
    })
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad()

    this.data.myData = perpage.data.preData

    if (this.data.myData) {
      let resStr = this.data.myData
      let strIndex = resStr.indexOf('goodsurl=') + 9
      let resParams = resStr.substr(strIndex)
      this.getCodeMsg(resParams)

    }
    this.getGoodsList()
    this.getPageData()


  },

  onLogin() {
    let that = this
    that.setData({
      showLogin: true
    })

  },


  //获取商品列表
  async getGoodsList() {
    let params = {
      sorttype: this.data.sortType,
      descasc: this.data.descasc,
      search: "",
      page: this.data.page,
      limit: this.data.limit
    }
    await goodList(params).then((res) => {

      if (res.errcode == 0) {
        this.setData({
          count: res.data.goodslist.count
        })
        if (this.data.onBottom == false) {
          this.setData({
            pointList: res.data.goodslist.goods
          })
        } else {
          let newArr = [...this.data.pointList, ...res.data.goodslist.goods]
          this.setData({
            pointList: newArr
          })
        }


      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
        });

      }
    })

  },

  async getUserInfo() {
    let that = this
    await getUser().then((res) => {

      if (res.errcode == 0) {
        that.setData({
          userInfo: res.user
        })

      }
      else if (res.errcode == 401) {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',

          success: (result) => {
            setTimeout(() => {
              wx.clearStorage({
                success: function (res) {
                  that.setData({
                    isJwt: false
                  })
                }
              })

            }, 1000);

          },

        });
      }
      else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
        });

      }

    })
  },

  //筛选条件
  onChooseTab(e) {
    this.data.shunxu == false ? this.setData({
      descasc: 'asc'
    }) : this.setData({
      descasc: 'desc'
    })

    this.setData({
      page: 1,
      sortType: e.currentTarget.dataset.item.type
    })
    const index = e.currentTarget.dataset.item.id;
    if (index == 2) {
      if (e.currentTarget.dataset.item.shunxu == false) {
        this.setData({
          shunxu: !this.data.shunxu
        })
      } else {
        this.setData({
          shunxu: !this.data.shunxu,
        })
      }
    } else if (index == 1) {
      if (e.currentTarget.dataset.item.shunxu == false) {
        this.setData({
          shunxu: !this.data.shunxu
        })
      } else {
        this.setData({
          shunxu: !this.data.shunxu,
        })
      }


    } else {
      this.setData({
        descasc: ''
      })
    }
    this.setData({ activeTab: index })
    this.getGoodsList()
  },

  handleSearch(e) {
    this.setData({
      searchValue: e.detail.value
    })

  },

  //搜索
  onSearch() {
    wx.navigateTo({ url: `/packageA/pages/searchpage/index?search=${this.data.searchValue}` })
  },

  onGoodsDetail(e) {
    wx.navigateTo({ url: `/packageA/pages/goodsdetail/index?goodsid=${e.currentTarget.dataset.item.id}` })
  },



  //扫码方法
  getScanCode() {
    wx.navigateTo({
      url: '/component/camerapage/index',
    });

  },
  //获取二维码数据
  async getCodeMsg(url) {
    let that = this
    let params = {
      goodsurl: url
    }
    await getCode(params).then((res) => {
      if (res.errcode == 0) {
        that.setData({
          showModal: true,
          fatherData: res.data,
          "fatherData.numRes": res.data.canscore * Number(res.data.multiplier),
          "fatherData.nexttime": (res.data.nexttime).substring(0, 10),
          "fatherData.endtime": (res.data.endtime).substring(0, 10),
          "fatherData.goodsurl": url,

        })

        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })



      } else {
        that.setData({
          showErrModal: true,
          errData: res.errmsg,
        })
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })


      }
    })
  },

  onReachBottom() {
    if (this.data.page * this.data.limit >= this.data.count) {
      this.setData({
        onBottom: false
      })
      wx.showToast({
        title: '没有更多了',
        icon: 'none',

      });

    } else {
      this.setData({
        onBottom: true
      })
      this.data.page++
      this.getGoodsList()
    }

  }

})
